package handler

import (
	"encoding/json"
	"oct13-exam/cache"
	"oct13-exam/model"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

// 仓库添加

func HouseAdd(c *gin.Context) {
	house_code := c.PostForm("house_code")
	house_name := c.PostForm("house_name")
	house_style := c.PostForm("house_style")

	house := model.House{
		HouseCode:  house_code,
		HouseName:  house_name,
		HouseStyle: house_style,
	}
	err := model.DB.Create(&house).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

// 订单添加

func OrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_name := c.PostForm("order_name")
	order_num := c.PostForm("order_num")
	hid, _ := strconv.Atoi(c.PostForm("hid"))

	order := model.Order{
		OrderCode: order_code,
		OrderName: order_name,
		OrderNum:  order_num,
		Hid:       hid,
	}
	err := model.DB.Create(&order).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

// 仓库订单列表

func HouseOrderList(c *gin.Context) {
	/*
		select
		orders.id as OrderId,
		orders.order_code as OrderCode,
		orders.order_name as OrderName,
		orders.order_num as OrderNum,
		houses.house_code as HouseCode,
		houses.house_name as HouseName,
		houses.house_style as HouseStyle
		from orders
		left join houses on houses.id = orders.hid
	*/
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var houseOrderList []model.HouseOrderList

	redis_key := "order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_name as OrderName",
				"orders.order_num as OrderNum",
				"houses.house_code as HouseCode",
				"houses.house_name as HouseName",
				"houses.house_style as HouseStyle",
			).
			Joins("left join houses on houses.id = orders.hid").
			Offset(offset).Limit(size).Debug().Find(&houseOrderList)
		jsonData, _ := json.Marshal(houseOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &houseOrderList)
	}

	var total int64
	model.DB.Model(&model.Order{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表获取成功",
		"data":  houseOrderList,
		"total": total,
	})
	return
}

// 仓库订单详情

func HouseOrderSelect(c *gin.Context) {
	id := c.Query("id")

	var houseOrderList model.HouseOrderList

	redis_key := "order_select_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_name as OrderName",
				"orders.order_num as OrderNum",
				"houses.house_code as HouseCode",
				"houses.house_name as HouseName",
				"houses.house_style as HouseStyle",
			).
			Joins("left join houses on houses.id = orders.hid").
			Where("orders.id = ?", id).
			Debug().Find(&houseOrderList)
		jsonData, _ := json.Marshal(houseOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &houseOrderList)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "搜索成功",
		"data": houseOrderList,
	})
	return
}
